<?
include_once IR.'functions/common/memcache.func.php';
include_once IR.'functions/common/ubbcode.func.php';

function user_get_user_account_by_uuid($uuid){
	$sql = "SELECT * FROM u_account WHERE uuid='$uuid'";
	$rst = mysql_x_query($sql);
	if ($user_account_info = mysql_fetch_assoc($rst)){
		return $user_account_info;
	} else {
		return array();
	}
}

function user_get_header_info($uid){
		return array();
}
function user_get_user_base($uid,$need_online=0){
	global $g_users_base;
	if (isset($g_users_base[$uid])){
	    return $g_users_base[$uid];
	}
	$user_info_base = get_cache($uid,'user_base_info',1);
        //$user_info_base = -1;
	if ($user_info_base ==-1 || $user_info_base ==-2) { 
	    $user_info_base = user_update_user_base($uid); 
	} else { 
	    $user_info_base = $user_info_base['data']; 
	}
	$g_users_base[$uid] = $user_info_base;
	return $user_info_base;

} 


function user_update_user_base($uid){ 
	global $g_users_base;  
	$sql = "SELECT * FROM u_account  WHERE uid = '$uid'";
	$rst = mysql_w_query($sql);
	if ($row_user_base = mysql_fetch_assoc($rst)){		 
		set_cache($uid,'user_base_info',$row_user_base,1);
		$g_users_base[$uid] = $row_user_base;
		return $row_user_base;
	} else {
		return false;
	} 
}

function user_get_user_vip_level($uid){
    global $zeit;
    $user_base = user_get_user_base($uid);
    if($user_base['vip_typ']!=0&&$user_base['vip_end_zeit']>$zeit){
        $vip_level = $user_base['vip_typ'];
    }else{
        $vip_level = 0;
    }
    return $vip_level;
}

function user_write_back_main_data($uid,$user_info,$change_list){
    global $g_users_base;
    foreach ($change_list as $key=>$val){
        $sql_plus[] = "$key = '".$user_info[$key]."' ";
    }
    if (count($sql_plus)==0){
        return;
    }
    $g_users_base[$uid] = $user_info;
    set_cache($uid,'user_base_info',$user_info,1);
    $sql = "UPDATE u_account SET ".implode(',',$sql_plus)." WHERE uid=$uid";
    mysql_x_query($sql);    
}

function user_write_back_extend_data($uid,$user_extend_info, $change_list=array()){
    global $g_users_extend,$g_user_extend;
    //$arr = array_diff_assoc($user_extend_info,$g_users_extend[$uid]);        
    $skip = array('week_day');
    $sql_plus = array();
    if(!empty($change_list)){
        foreach ($change_list as $this_key=>$this_val){
            if(isset($user_extend_info[$this_key])){
                if($user_extend_info[$this_key] != $this_val){
                    $user_extend_info[$this_key] = $this_val;
                    if($this_key=='week_day'){
                        continue;
                    }
                    $sql_plus[] = "$this_key = '".$this_val."'";
                }
            }
        }
    }else{
        $arr = array_diff_assoc($user_extend_info,$g_users_extend[$uid]);
        foreach($arr as $this_key=>$val){
            if($this_key=='week_day'){
                continue;
            }else{
                $sql_plus[] = "$this_key = '".$user_extend_info[$this_key]."'";
            }
        }
    }
    if (count($sql_plus)==0){
        return $user_extend_info;
    }
    $g_users_extend[$uid] = $user_extend_info;
    $g_user_extend = $user_extend_info;
    set_cache($uid,'user_extend_info',$user_extend_info,1); 
    $sql = "UPDATE u_user_extend SET ".implode(',',$sql_plus)." WHERE uid=$uid";  
    mysql_x_query($sql);
    return $user_extend_info;
}

function user_get_user_extend($uid,$need_online=0){
    global $g_users_extend,$zeit,$cfg_refresh_energy_zeit;
    if (isset($g_users_extend[$uid])){
        return $g_users_extend[$uid];
    }
    $user_info_extend = get_cache($uid,'user_extend_info',1);
    if ($user_info_extend ==-1 || $user_info_extend ==-2) { 
        $user_info_extend = user_update_user_extend($uid);
    } else {
        $user_info_extend = $user_info_extend['data'];
    } 
    $g_users_extend[$uid] = $user_info_extend;
    return $user_info_extend;
}

function user_update_user_extend($uid){
	global $zeit,$g_sunrise;
	global $g_users_extend;	
	$sql = "SELECT * FROM u_user_extend WHERE uid = '$uid'";
	$rst = mysql_x_query($sql);
	if ($row_user_base = mysql_fetch_assoc($rst)){
        set_cache($uid,'user_extend_info',$row_user_base,1);
        $g_users_extend[$uid] = $row_user_base;
        return $row_user_base;
	} else {
		return false;
	}

}

function user_delete_user($uid){
    global $zeit;
    //目前只考虑删除未过新手期的用户，牵扯到的表较少
    $sql = "SELECT pid FROM p_player WHERE cid = $uid";
    $rst = mysql_x_query($sql);
    $pids = array();
    while ($row = mysql_fetch_assoc($rst)){
        $pids[] = $row['pid'];
    }
    if (empty($pids)){
        return -1;
    }
    $pid_list = implode(',',$pids);
    
    $sql = "DELETE FROM p_player WHERE pid IN ($pid_list)";
    mysql_x_query($sql);
    $sql = "DELETE FROM p_player_base_attr WHERE pid IN ($pid_list)";
    mysql_x_query($sql);
    $sql = "DELETE FROM p_player_final_attr WHERE pid IN ($pid_list)";
    mysql_x_query($sql);
    $sql = "DELETE FROM p_player_middle_attr WHERE pid IN ($pid_list)";
    mysql_x_query($sql);
    $sql = "DELETE FROM p_player_skill WHERE pid IN ($pid_list)";
    mysql_x_query($sql);
    $sql = "DELETE FROM p_player_train WHERE pid IN ($pid_list)";
    mysql_x_query($sql);
    
    
    $sql = "DELETE FROM p_player_youth WHERE  cid=$uid";
    mysql_x_query($sql);
    $sql = "DELETE FROM p_player_youth_study WHERE  cid=$uid";
    mysql_x_query($sql);
    $sql = "DELETE FROM p_player_youth_study_site WHERE cid=$uid";
    mysql_x_query($sql);
    
    
    $sql = "DELETE FROM c_club_info WHERE cid=$uid";
    mysql_x_query($sql);
    $sql = "DELETE FROM c_formation WHERE cid=$uid";
    mysql_x_query($sql);
    $sql = "DELETE FROM c_fixture WHERE cid=$uid OR op_cid=$uid";
    mysql_x_query($sql); 
    $sql = "DELETE FROM c_squad WHERE cid=$uid";
    mysql_x_query($sql);
    $sql = "DELETE FROM c_tactic WHERE cid=$uid";
    mysql_x_query($sql);
    $sql = "DELETE FROM c_team_employees WHERE cid=$uid";
    mysql_x_query($sql);
    
    $sql = "DELETE FROM st_player_statistics WHERE cid=$uid";
    mysql_x_query($sql);
    
    
    $sql = "DELETE FROM c_ins_history WHERE cid=$uid";
    mysql_x_query($sql);
    $sql = "DELETE FROM c_ins_league_table WHERE ins_uid=$uid";
    mysql_x_query($sql);
    $sql = "DELETE FROM c_ins_match_fixture WHERE ins_uid=$uid";
    mysql_x_query($sql);
    
    $sql = "DELETE FROM e_user WHERE uid=$uid";
    mysql_x_query($sql);
    $sql = "DELETE FROM feed_detail_user_news WHERE uid=$uid";
    mysql_x_query($sql);    
    $sql = "DELETE FROM t_task WHERE uid=$uid";
    mysql_x_query($sql);
    $sql = "DELETE FROM t_task_step WHERE uid=$uid";
    mysql_x_query($sql);
    $sql = "DELETE FROM u_user_extend WHERE uid=$uid";
    mysql_x_query($sql);    
    $sql = "DELETE FROM u_item WHERE cid=$uid";
    mysql_x_query($sql);
    
    
    $sql = "UPDATE u_account SET pp_uid='DELETED' WHERE uid=$uid";
    mysql_x_query($sql);
    $sql = "UPDATE u_account SET pp_uid='DELETED',DOA=-1,DOA_zeit=$zeit WHERE uid=$uid";
    mysql_x_query($sql);
    
    delete_cache($uid,'user_base_info');
    delete_cache($uid,'user_extend_info');
    return;
}

function send_chat($typ,$uid,$pf,$uname,$data,$zeit){
    global  $zone_id;
    $new_data = array('typ'=>$typ,'uid'=>$uid,'pf'=>$pf,'uname'=>$uname,'data'=>$data,'zeit'=>$zeit,'zeit_show'=>format_date_time($zeit,3));    
    $chat_cache = get_cache($zone_id,'sys_chat_world',0);
    $chat_id = 1;
    if ($chat_cache == -1 || $chat_cache==-2){
        $chat_cache = array();
    } else {
        $chat_cache = $chat_cache['data'];
        $chat_count = count($chat_cache);
        $chat_id = $chat_cache[$chat_count-1]['chat_id']+1;
        if ($chat_count>=15){
            array_shift($chat_cache);
        }  
    }
    $new_data['chat_id'] = $chat_id;
    $chat_cache[] =  $new_data;
    
    set_cache($zone_id,'sys_chat_world',$chat_cache,0);
    return $chat_cache;
}


function write_st_pay_real_credits($uid,$typ,$cost){
    //1: 升级装备
    //2: 装备冷却加速
    //3: 球迷爆棚
    //4：加速挂机
    //5：高级挂机
    //6：俱乐部改名
    //7：赛前恢复体力
    //8：黑市刷球员
    //9: 结束特训
    //10: 青训球员改名
    //11 转生
    //12 开特训位
    //13 洗点
    //14 市场球探刷球员
    //15 商城买道具
    //16 战术位解锁
    //17 加速青训留学
    //18 刷青训球员
    //19 玩家市场交易
    //20 重复刷洗点
    //21 合成保护 代替幸运符
    //22 经验转移
    //23 每日活动buff
    //24 公会捐献
    //25 商城买球员包
    //26 商城买其他
    //27 快速特训
    //28 买两次实况杯
    //29 每日活动
    //30 购买VIP
    //31 7级送球员活动
    //32 经典球员续约
    //33 抽奖活动
    //34 买DNA
    //35 开青训位
    //36 锁定青训位 
    //37赞助商签约
    //38跳过比赛
    //39 5元礼包
    //40 锁定周赛报名
    //41 个性球场
    //42 建立杰宝杯
    //43 购买技能石
    //101 押注
    //102 押注收益
    global $g_sunrise;
    $club_info = club_get_club_info($uid);
	if ($club_info['is_robot']>0){
		//游戏托不计入统计
		return;
	}
    $sql = "INSERT INTO st_pay_real_credits
    (zeit,cid,typ,cost,level)
    VALUES
    ($g_sunrise,$uid,$typ,$cost,$club_info[rating_lev])";
    mysql_x_query($sql);
}